package org.example;

import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.sql.*;
import java.util.Properties;

public class JdbcInsertCode2 {
    static Connection con;
    static {
        Properties properties = new Properties();
        try {
            properties.load(new BufferedReader(new FileReader("first_git/src/main/resources/local_mysql.properties")));
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
        String Driver = properties.getProperty("Driver");
        String url = properties.getProperty("url");
        String user = properties.getProperty("user");
        String password = properties.getProperty("password");
        try {
            Class.forName(Driver);
        } catch (ClassNotFoundException e) {
            throw new RuntimeException(e);
        }
        try {
            con = DriverManager.getConnection(url,user,password);
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
    }
    public static void insert() throws IOException, SQLException {
        BufferedReader bufferedReader = new BufferedReader(new FileReader("first_git/src/data/cars.csv"));
        String values="";
        int count=0;
        int num=0;
        String lines;
        long star = System.currentTimeMillis();
        //38.77秒 1000一组
        //23.524秒 500一组
        while ((lines=bufferedReader.readLine())!=null){
            String[] split = lines.split(",");
            values=String.format("%s('%s','%s','%s','%s','%s','%s','%s','%s','%s'),",values,split[0],split[1],split[2],split[3],split[4],split[5],split[6],split[7],split[8]);
            count++;
            if (count%500==0){
                num+=1000;
                String sql ="insert into car_info values "+values;
                sql = sql.substring(0,sql.length()-1);
                Statement statement = con.createStatement();
                statement.executeUpdate(sql);
                count=0;
                values="";
                System.out.println("第"+num+"行数据插入成功");
            }
        }
        long end =System.currentTimeMillis();
        System.out.println("总用时："+(end-star)/1000.0+"秒");

    }

    public static void main(String[] args) throws SQLException, IOException {
        insert();
    }
}
